'use strict';
/////////////////////////////////////
// 表格抽象化 yxtable14版本 重构版
// =====================
// liuyp 2019年5月21日17:06:05
/////////////////////////////////////
function yxtable14($location, $ctrlCache) {
    var tableInit = function($scope){
        var urlParam = $location.search();
        $scope.objBack = angular.copy($scope.obj); // 备份一个搜索项默认值
        angular.extend($scope.obj, urlParam); // 获取url存储的内容
        if( $scope.pagesize ){
            $scope.page = urlParam.page_index || $scope.page;
        }
        var orderKeySave = urlParam.orderKey || '';
        var orderTypeSave = urlParam.orderType || 0;
        $scope.params = angular.copy($scope.obj); // 准备一个仅供搜索的对象
        $scope.showMessage = function() {
            var page = $scope.page;
            var pagesize = $scope.pagesize;
            var totalCount = $scope.yxt14totalrow;
            if(totalCount == 0) {return"当前第0 - 0条，共0条"}
            if(totalCount == undefined) { return"当前第 0 条"}
            var start_page = (page - 1) * pagesize + 1;
            var end_page = (page * pagesize < totalCount) ? page * pagesize : totalCount;
            return"当前第" + start_page +" -" + end_page +"条，共" + totalCount +"条";
        };
        function doOneReadCache(){
            var cache = $ctrlCache.getCache();
            if (cache) angular.extend($scope.params, cache);
            doOneReadCache = function(){};
        }
        $scope.pageChanged = function(page){ // 分页
            if( $scope.pagesize ){
                if(page) { $scope.page = page; }
                $scope.params.page_index = $scope.page;
                $scope.params.page_size = $scope.pagesize;
            }
            doOneReadCache();
            angular.extend($scope.obj, $scope.params);
            $location.search($scope.params); // 将所有搜索项存入url
            $scope.showList();
        }
        $scope.search = function(){ // 搜索
            angular.extend($scope.params, $scope.obj);
            $scope.pageChanged(1);
        }
        $scope.reset = function(){ // 重置
            $scope.obj = angular.copy($scope.objBack); // 恢复搜索项为备份
            $scope.params = angular.copy($scope.objBack);
            $location.search({}); // 将所有搜索项存入url
            $scope.pageChanged(1);
        }
        $scope.order = function(orderKey){ // 搜索
            var k = parseInt(orderTypeSave)
            if( orderKeySave == orderKey ){
                orderTypeSave = (k>=2)? 0 : k+1
            }else{
                orderKeySave = orderKey;
                orderTypeSave = 1;
            }
            $scope.params.orderKey = orderKeySave;
            $scope.params.orderType = orderTypeSave;
            $scope.pageChanged(1);
        }
        // 初始化
        $scope.pageChanged();
    };
    return {init: tableInit}
}
exports = module.exports = yxtable14;